package org.itboys.mobile.service.mongo.site;

import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.entity.mongo.SundayNews;
import org.itboys.mobile.entity.mongo.SundayNewsCategory;
import org.itboys.mobile.service.mongo.web.SundayNewsCategoryService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.Sun;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/10/23
 * 描述：雄安加盟网_
 */
@Service
public class SundaySiteNewsService extends BaseMongoService<SundayNews>{
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Autowired
    private SundayNewsCategoryService categoryService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayNews> getEntityClass() {
        return SundayNews.class;
    }

    public PageResult<SundayNews> select(HttpServletRequest request,Long categoryId,Integer page){

        Map<String,Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", 0);
        param.put("categoryId",categoryId);
        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"title");
        PageResult<SundayNews> result=super.containpageQuery(request,containsparam,param);
        return result;
    }

    /**
     * 获取推荐的前五个
     * @return
     */
    public List<SundayNews> getRecommendNews(){
        Map<String,Object> param=new HashMap<String ,Object>();
        param.put("isDeleted", 0);
        param.put("isRecommend",1);
        List<SundayNews> newss=super.list(param,5);
        return  newss;
    }

    /**
     * 获取首页需要的资讯列表
     * @return
     */
    public Map<String,Object> getIndexNews(){
        Map<String,Object> result=new HashMap<String,Object>();
        Map<String,Object> param=new HashMap<String ,Object>();
        List<SundayNewsCategory> indexCategories=categoryService.getIndexNewsCategory();
        if(ListUtil.isNotNull(indexCategories)){
            List<SundayNews> newss=null;
            List<SundayNews> allNews=new ArrayList<SundayNews>();
            for(SundayNewsCategory category:indexCategories){
                param.clear();
                param.put("isDeleted", 0);
                param.put("isIndex",1);
                param.put("categoryId",category.getId());
                newss=super.list(param,5);
                allNews.addAll(newss);
            }
            result.put("indexNewss",allNews);
        }
        List<SundayNewsCategory> recommendCategories=categoryService.getIndexRecommendNewsCategory();
        if(ListUtil.isNotNull(recommendCategories)){
            List<SundayNews> newss=null;
            List<SundayNews> allNews=new ArrayList<SundayNews>();
            for(SundayNewsCategory category:recommendCategories){
                param.clear();
                param.put("isDeleted", 0);
                param.put("isRecommend",1);
                param.put("categoryId",category.getId());
                newss=super.list(param,8);
                allNews.addAll(newss);
            }
            result.put("recommendNewss",allNews);
        }

        return result;

    }

    /**
     * 获取首页推荐的资讯
     * @return
     */
    public List<SundayNews> getIndexRecommendNews(){
        List<SundayNews> allNews=new ArrayList<SundayNews>();
        Map<String,Object> param=new HashMap<String ,Object>();
        List<SundayNewsCategory> categories=categoryService.getIndexRecommendNewsCategory();
        if(ListUtil.isNotNull(categories)){
            List<SundayNews> newss=null;
            for(SundayNewsCategory category:categories){
                param.put("isDeleted", 0);
                param.put("isRecommend",1);
                param.put("categoryId",category.getId());
                newss=super.list(param,5);
                allNews.addAll(newss);
            }
            return allNews;
        }else {
            return null;
        }

    }
    /**
     * 获取相关资讯
     * @param id
     * @return
     */
    public List<SundayNews> getXiangguangNews(Long id){
        SundayNews news=super.getById(id);
        Map<String,Object> param=new HashMap<String ,Object>();
        param.put("isDeleted", 0);
        param.put("categoryId",news.getCategoryId());
        param.put("id !=",news.getId());
        return super.list(param,6);
    }

}
